package com.aspose.words.maven.wizard;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;

import freemarker.template.Template;

public class ResourceFactory {
	
	private final static String resourcePath = "platform:/plugin/AsposeWordsMavenPlugin/com/aspose/words/maven/wizard/files/";

	static InputStream getResourceAsInputStream(AsposeWizardResources resource){
		try{
			switch (resource) {
				case BANNER_IMG:
					return new URL(resourcePath + "long_banner.png")
							.openConnection().getInputStream();
				default:
					return null;
			}
		}catch(MalformedURLException e){
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	static Template getTemplate(AsposeWizardResources resource){
		final InputStream is;
		final BufferedReader br;
		try {
			switch (resource) {
				case MAVEN_PROJECT_STRUCTURE_TEMPLATE:
					 is = new URL(resourcePath + "maven_project_structure.ftl")
						.openConnection().getInputStream();
						 br = new BufferedReader(new InputStreamReader(is));
						return new Template("maven_project_structure_template", br);
				case POM_FILE_TEMPLATE:
					 is = new URL(resourcePath + "pom.ftl")
						.openConnection().getInputStream();
						 br = new BufferedReader(new InputStreamReader(is));
						return new Template("pom_file_template", br);
				default:
					return null;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	static String getMavenProjectStructureTemplate(){
		final StringBuilder sb = new StringBuilder();
		 try(InputStream is = 
				 new URL(resourcePath + "maven_project_structure.ftl")
					.openConnection().getInputStream();
			 BufferedReader br = new BufferedReader(new InputStreamReader(is))){
			 int character;
			 while( (character = br.read()) != -1 ){
				 sb.append((char) character);
			 }
			 return sb.toString();
		 } catch (IOException e) {
			e.printStackTrace();
			return sb.toString();
		}
	}
	
	enum AsposeWizardResources {
		POM_FILE_TEMPLATE, MAVEN_PROJECT_STRUCTURE_TEMPLATE, BANNER_IMG
	}
}